package com.hot12345.springmvc.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.hot12345.common.spring.SpringAbstractController;
import com.hot12345.common.util.MD5;
import com.hot12345.dao.ItemDaoImpl;
import com.hot12345.dao.UserDaoImpl;
import com.hot12345.entity.Item;
import com.hot12345.entity.User;

@Controller
@RequestMapping("/admin/user")
public class UserController extends SpringAbstractController {
	
	private final static Log log = LogFactory.getLog(UserController.class);

	@RequestMapping("/index")
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		UserDaoImpl usereDao = UserDaoImpl.getInstance();
		pageBefore(request, modelMap);
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("state_gte", 0);
		
		
		List<User> userList = usereDao.findByParamsPage(params, getStartNo(modelMap), getPageSize(modelMap));
		long totalNum = usereDao.findCount(params);
		
		pageAfter(request, totalNum, modelMap);
		
		modelMap.put("userList", userList);
		
		return new ModelAndView("admin/user/userList");
	}
	
	@RequestMapping("/add")
	public ModelAndView add(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		return new ModelAndView("admin/user/userAdd");
	}
	
	@RequestMapping("/insert")
	public void insert(HttpServletRequest request,
			HttpServletResponse response, 
			ModelMap modelMap,
			@RequestParam("name") String name,
			@RequestParam("password") String password,
			@RequestParam("password1") String password1) throws Exception {
		
		if(password1.length() <6 || password.length()>20 || password.length()!=password1.length()){
			log.error("密码不正确");
		}
		
		User user = new User();
		user.setName(name);
		user.setPassword(MD5.encrypt(password));
		user.setState(1);
		
		UserDaoImpl userDao = UserDaoImpl.getInstance();
		userDao.insert(user);
		
		response.sendRedirect("/admin/user/index");
	}
	
	/**
	 * 冻结
	 *	
	 * @param request
	 * @param response
	 * @param modelMap
	 * @param id
	 * @throws Exception
	 * @author wuzongbao
	 * @date 2013-3-3 下午11:48:23
	 * @version V1.0
	 */

	@RequestMapping(value="/freeze", method = RequestMethod.GET)
	public void freeze(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,
			int id) throws Exception {
		
		UserDaoImpl userDao = UserDaoImpl.getInstance();
		User user = userDao.findByPrimaryKey(id);
		user.setState(0);
		userDao.update(user);
		
		response.sendRedirect("/admin/user/index");
	}
	
	/**
	 * 恢复正常状态
	 *	
	 * @param request
	 * @param response
	 * @param modelMap
	 * @param id
	 * @throws Exception
	 * @author wuzongbao
	 * @date 2013-3-3 下午11:48:43
	 * @version V1.0
	 */
	@RequestMapping(value="/restore", method = RequestMethod.GET)
	public void restore(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,
			int id) throws Exception {
		
		UserDaoImpl userDao = UserDaoImpl.getInstance();
		User user = userDao.findByPrimaryKey(id);
		user.setState(1);
		userDao.update(user);
		
		response.sendRedirect("/admin/user/index");
	}
}
